package com.seuic.hnjiningposapp.hnapps.ui.view.dialog;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.core.CenterPopupView;
import com.seuic.hnjiningposapp.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class ActivationInputPopup extends BaseCenterPopupView {//激活输入弹窗
    private final ClickCallback callevent;
    Context context;
    Activity activity;

    @BindView(R.id.activation_username)
    EditText activationUsername;//用户名
    @BindView(R.id.activation_password)
    EditText activationPassword;//密码
    @BindView(R.id.activation_organizationcode)
    EditText activationOrganizationcode;//核心机构号
    @BindView(R.id.activation_devicecode)
    EditText activationDevicecode;//设备编号
    @BindView(R.id.activation_secretkey1)
    EditText activationSecretkey1;//秘钥分量1
    @BindView(R.id.activation_secretkey2)
    EditText activationSecretkey2;//秘钥分量2
    @BindView(R.id.import_moneynum_gz)
    Button importMoneynumGz;//更正
    @BindView(R.id.import_moneynum_ok)
    Button importMoneynumOk;//确定
    @BindView(R.id.import_moneynum_layout)
    LinearLayout importMoneynumLayout;
    @BindView(R.id.username_layout)
    LinearLayout usernameLayout;
    @BindView(R.id.password_layout)
    LinearLayout passwordLayout;
    @BindView(R.id.organizationcode_layout)
    LinearLayout organizationcodeLayout;
    @BindView(R.id.devicecode_layout)
    LinearLayout devicecodeLayout;
    @BindView(R.id.secretkey1_layout)
    LinearLayout secretkey1Layout;
    @BindView(R.id.secretkey2_layout)
    LinearLayout secretkey2Layout;
    private EditText editable;
    private OnFocusChangeListener mFocusChangedListener;
    private String TdgBrah;
    private String OperTellerNo;
    private String TellerPwd;
    private String DvcNum;
    Activity mActivity;

    public ActivationInputPopup(Context context, Activity activity, ClickCallback clickCallback) {
        super(context);
        this.callevent = clickCallback;
        this.mActivity = activity;
    }

    public interface ClickCallback {
        /**
         * @param TdgBrah      机构号
         * @param OperTellerNo 交易柜员号
         * @param TellerPwd    柜员密码
         * @param DvcNum       柜员密码
         * @param key1         分量秘钥1
         * @param key2         分量秘钥2
         */
        void onCallToSingIn(String TdgBrah, String OperTellerNo, String TellerPwd, String DvcNum, String key1, String key2);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_activationinput;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        importMoneynumOk.setText("下一步");
        mFocusChangedListener = (v, hasFocus) -> {
            if (hasFocus) {
                editable = (EditText) v;
            }
        };
        activationUsername.setOnFocusChangeListener(mFocusChangedListener);
        activationPassword.setOnFocusChangeListener(mFocusChangedListener);
        activationOrganizationcode.setOnFocusChangeListener(mFocusChangedListener);
        activationDevicecode.setOnFocusChangeListener(mFocusChangedListener);
        activationSecretkey1.setOnFocusChangeListener(mFocusChangedListener);
        activationSecretkey2.setOnFocusChangeListener(mFocusChangedListener);

        activationDevicecode.setText("");
    }

    @OnClick({R.id.import_moneynum_gz, R.id.import_moneynum_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.import_moneynum_gz:
                editable.setText("");
                break;
            case R.id.import_moneynum_ok:
                if (!EdIsEmpty() && importMoneynumOk.getText().toString().equals("下一步")) {
                    usernameLayout.setVisibility(View.GONE);
                    passwordLayout.setVisibility(View.GONE);
                    organizationcodeLayout.setVisibility(View.GONE);
                    devicecodeLayout.setVisibility(View.GONE);

                    secretkey1Layout.setVisibility(VISIBLE);
                    secretkey2Layout.setVisibility(VISIBLE);


                    TdgBrah = activationOrganizationcode.getText().toString();
                    OperTellerNo = activationUsername.getText().toString();
                    TellerPwd = activationPassword.getText().toString();
                    DvcNum = activationDevicecode.getText().toString();

                    importMoneynumOk.setText("激活");
                } else if (importMoneynumOk.getText().toString().equals("激活")) {
                    if (activationSecretkey1.getText().toString().equals("")) {
                        ToastUtils.showLong("请输入密钥分量1");
                        return;
                    } else if (activationSecretkey2.getText().toString().equals("")) {
                        ToastUtils.showLong("请输入密钥分量2");
                        return;
                    } else if (activationSecretkey1.getText().toString().trim().length() != 32) {
                        ToastUtils.showLong("密钥分量1长度不正确");
                        return;
                    } else if (activationSecretkey2.getText().toString().trim().length() != 32) {
                        ToastUtils.showLong("密钥分量2长度不正确");
                        return;
                    } else {
                        String key1 = activationSecretkey1.getText().toString();
                        String key2 = activationSecretkey2.getText().toString();
                        callevent.onCallToSingIn(TdgBrah, OperTellerNo, TellerPwd, DvcNum, key1, key2);
                    }
                }
                break;
            default:
                break;
        }
    }


    public void recover() {
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                usernameLayout.setVisibility(View.VISIBLE);
                passwordLayout.setVisibility(View.VISIBLE);
                organizationcodeLayout.setVisibility(View.VISIBLE);
                devicecodeLayout.setVisibility(View.VISIBLE);

                secretkey1Layout.setVisibility(GONE);
                secretkey2Layout.setVisibility(GONE);

                importMoneynumOk.setText("下一步");
            }
        });

    }

    public void recoverActivate() {
        usernameLayout.setVisibility(View.GONE);
        passwordLayout.setVisibility(View.GONE);
        organizationcodeLayout.setVisibility(View.GONE);
        devicecodeLayout.setVisibility(View.GONE);

        secretkey1Layout.setVisibility(VISIBLE);
        secretkey2Layout.setVisibility(VISIBLE);

        importMoneynumOk.setText("激活");
    }

    private boolean EdIsEmpty() {
        if (activationUsername.getText().toString().equals("")) {
            ToastUtils.showLong("请输入用户名");
            return true;
        } else if (activationPassword.getText().toString().equals("")) {
            ToastUtils.showLong("请输入密码");
            return true;
        } else if (activationOrganizationcode.getText().toString().equals("")) {
            ToastUtils.showLong("请输入核心机构号");
            return true;
        } else if (activationDevicecode.getText().toString().equals("")) {
            ToastUtils.showLong("请输入设备号");
            return true;
        } else {
            return false;
        }
    }

}